package com.sunshine.wrongquestionset.activity.main

import android.app.Application
import android.util.TimeUtils
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import com.sunshine.wrongquestionset.room.entity.*
import com.sunshine.wrongquestionset.room.repository.MyRepository
import java.util.*

/**
 * @author sunshine
 * @date 2021/3/28
 */
class MainViewModel(application: Application): AndroidViewModel(application) {
    private val repository: MyRepository = MyRepository(application.applicationContext)

    fun getSubject(userId: String): LiveData<List<SubjectEntity>?> {
        return repository.getAllSubject(userId)
    }

    fun getQuestionSet(userId: String): LiveData<List<QuestionSetEntity>?> {
        return repository.getQuestionSetByUserId(userId)
    }

    fun getQuestion(userId: String): LiveData<List<QuestionEntity>?> {
        return repository.getUserAllQuestion(userId)
    }

    fun getTodayWrongQuestion(): LiveData<List<WrongQuestionBean>?> {
        val today = Date()
        return repository.getWrongQuestionBeanInDate(today.time - com.sunshine.wrongquestionset.utils.TimeUtils.DAY_LONG, today.time)
    }

    fun getAllQuestionInfo(): LiveData<List<QuestionInfoEntity>?> {
        return repository.getAllQuestionInfo()
    }

    /**
     * 获取所有用户的问题
     * 这个只用于更新问题的熟悉程度使用
     * 因为一个用户不应该处理其他用户的问题
     */
    fun getAllUserQuestion(): LiveData<List<QuestionEntity>?> {
        return repository.getAllQuestion()
    }

    fun getQuestionById(questionId: Int): LiveData<QuestionEntity?> {
        return repository.getQuestionById(questionId)
    }

    fun getQuestionType(typeId: Int): LiveData<String?> {
        return repository.getTypeNameById(typeId)
    }

    //获取某学科中错题本的数目
    fun getQuestionSetCountBySubjectId(subjectId: Int): LiveData<Int> {
        return repository.getQuestionSetCountInSubjectId(subjectId)
    }

    /**
     * 更新配置类
     */
    fun updateAppConfig(appConfigEntity: AppConfigEntity) {
        repository.updateAppConfigByEntity(appConfigEntity)
    }

    fun refreshFamiliarity(questionId: Int, familiarity: Float) {
        repository.updateQuestionFamiliarity(questionId, familiarity)
    }

    fun insertQuestionInfo(questionId: Int, familiarity: Float) {
        repository.insertQuestionInfo(questionId, familiarity)
    }

    fun addSubject(userId: String, name: String) {
        repository.insertSubject(userId, name)
    }

    fun toDelete(subjectEntity: SubjectEntity?) {
        repository.deleteSubjectByEntity(subjectEntity)
    }

    fun updateSubject(subjectEntity: SubjectEntity?) {
        repository.updateSubject(subjectEntity)
    }

    fun deleteQuestion(questionEntity: QuestionEntity) {
        repository.deleteQuestionByEntity(questionEntity)
    }

    fun getWrongQuestionByQuestionId(questionId: Int): LiveData<List<WrongQuestionEntity>?> {
        return repository.getWrongQuestionByQuestion(questionId)
    }

    fun getWrongQuestionByUserId(userId: String): LiveData<List<WrongQuestionBean>?> {
        return repository.getWrongQuestionBeanByUserId(userId)
    }

    fun getAllQuestionType(): LiveData<List<QuestionTypeEntity>?> {
        return repository.getAllQuestionType()
    }

    fun getQuestionBySubject(subjectId: Int): LiveData<List<QuestionEntity>?> {
        return repository.getQuestionBySubject(subjectId)
    }

    fun getSubjectById(subjectId: Int): LiveData<SubjectEntity?> {
        return repository.getSubjectById(subjectId)
    }

    fun getQuestionInfoByQuestionId(questionId: Int): LiveData<List<QuestionInfoEntity>?> {
        return repository.getQuestionInfoByQuestionId(questionId)
    }

    fun getWrongQuestionBeanByQuestionId(questionId: Int): LiveData<List<WrongQuestionBean>?> {
        return repository.getWrongQuestionBeanByQuestionId(questionId)
    }

    fun getUserById(userId: String): LiveData<UserEntity?> {
        return repository.getUser(userId)
    }
}